package cn.tianjindong.blog_portal.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tianjindong.blog.common.jedis.JedisClient;
import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.mapper.ArticleTagMapper;
import cn.tianjindong.blog.mapper.BlogArticleCommentMapper;
import cn.tianjindong.blog.mapper.BlogArticleMapper;
import cn.tianjindong.blog.mapper.BlogUserCollectionMapper;
import cn.tianjindong.blog.pojo.ArticleTagExample;
import cn.tianjindong.blog.pojo.BlogArticle;
import cn.tianjindong.blog.pojo.BlogArticleCategory;
import cn.tianjindong.blog.pojo.BlogArticleCommentExample;
import cn.tianjindong.blog.pojo.BlogUserCollectionExample;
import cn.tianjindong.blog_portal.service.ArticleService;

@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private BlogArticleMapper articleMapper;

	@Autowired
	private BlogUserCollectionMapper userCollectionMapper;

	@Autowired
	private ArticleTagMapper articleTagMapper;

	@Autowired
	private BlogArticleCommentMapper commentMapper;

	@Autowired
	private JedisClient jedisClient;

	private static final String VISIT_PREFIX = "USER_VISIT_";// 标志用户访问的前缀
	
	private static final String VISIT_NUM="ARTICLE_VISIT_NUM_";

	private static final int EXPIRE_TIME = 1800;//（访问次数）缓存过期时间为

	@Override
	public BlogArticle getArticleByIdAndAlias(Long id, String alias, String jsessionid) {
		BlogArticle article = articleMapper.getArticleById(id);
		BlogArticleCategory category = null;
		if (article != null) {
			category = article.getCategory();
			if (category != null && category.getAlias().equals(alias)) {
				// 组合key "ARTICLE"+文章ID+JESSIONID
				StringBuffer key = new StringBuffer(VISIT_PREFIX);
				key.append(article.getId());
				key.append(jsessionid);
				boolean exists = jedisClient.exists(key.toString());
				if (exists) {// 如果存在则更新缓存时间
					jedisClient.expire(key.toString(), EXPIRE_TIME);
				} else {//如果不存在则更新访问次数
					BlogArticle tmpArticle = new BlogArticle();
					tmpArticle.setId(article.getId());
					tmpArticle.setVisitNum(article.getVisitNum()+1);
					articleMapper.updateByPrimaryKeySelective(tmpArticle);
					
					jedisClient.set(key.toString(), key.toString(), EXPIRE_TIME);
					article.setVisitNum(article.getVisitNum()+1);
				}

				// 获取评论数量
				BlogArticleCommentExample example = new BlogArticleCommentExample();
				example.createCriteria().andArticleIdEqualTo(article.getId());
				int count = commentMapper.countByExample(example);
				article.setCommentNum(count);
				return article;
			}
		}
		return null;
	}

	@Override
	public BlogArticle getArticleById(Long id,String jsessionid) {
		BlogArticle article = articleMapper.getArticleById(id);
		// 组合key "ARTICLE"+文章ID+JESSIONID
		StringBuffer key = new StringBuffer(VISIT_PREFIX);
		key.append(article.getId());
		key.append(jsessionid);
		boolean exists = jedisClient.exists(key.toString());
		if (exists) {// 如果存在则更新缓存时间
			jedisClient.expire(key.toString(), EXPIRE_TIME);
		} else {//如果不存在则更新访问次数
			BlogArticle tmpArticle = new BlogArticle();
			tmpArticle.setId(article.getId());
			tmpArticle.setVisitNum(article.getVisitNum()+1);
			articleMapper.updateByPrimaryKeySelective(tmpArticle);
			jedisClient.set(key.toString(), key.toString(), EXPIRE_TIME);
			article.setVisitNum(article.getVisitNum()+1);
		}
		return article;
	}

	@Override
	public List<BlogArticle> getArticleListOrderByVisitNum(Integer countItem) {
		List<BlogArticle> list = articleMapper.getBriefListAndTabAndOrderByVisitNum(0, countItem);
		return list;
	}

	@Override
	public List<BlogArticle> getArticlePageView(Integer currentPage, Integer countItem) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogArticle> list = articleMapper.getBriefListOrderByUpdateTime(startIndex, countItem);
		return list;
	}

	@Override
	public PageView<BlogArticle> getCollectionArticlePageView(Long userId, Integer currentPage, Integer countItem) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogArticle> list = articleMapper.getCollectArticleListLimitAndTab(userId, startIndex, countItem);
		// 获取数据库总条数
		BlogUserCollectionExample example = new BlogUserCollectionExample();
		example.createCriteria().andUserIdEqualTo(userId);
		int totalItem = userCollectionMapper.countByExample(example);

		PageView<BlogArticle> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);
		return pageView;
	}

	@Override
	public PageView<BlogArticle> getArticlePageViewByTagId(Long tagId, Integer currentPage, Integer countItem) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogArticle> list = articleMapper.getTagArticleListLimitAndTab(tagId, startIndex, countItem);
		// 获取数据库总条数
		ArticleTagExample example = new ArticleTagExample();
		example.createCriteria().andTagIdEqualTo(tagId);
		int totalItem = articleTagMapper.countByExample(example);

		PageView<BlogArticle> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);
		return pageView;
	}
}
